package com.powell.health.fragment;

import android.app.ListFragment;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.powell.health.activity.ItemEntryActivity;
import com.powell.health.activity.R;
import com.powell.health.dao.sqlhelper.SqlHealthManagerDatabaseHelper;
import com.powell.health.dao.table.CategoryTable;
import com.powell.health.item.Item;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: tpowell
 * Date: 12/28/12
 * Time: 11:14 AM
 * To change this template use File | Settings | File Templates.
 */
public class ItemListFragment extends ListFragment {

//    private List<Item> listItem = new ArrayList<Item>();


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        List<String> listItem = new ArrayList<String>();
        populateListItem(listItem);

        setListAdapter(new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_activated_1, listItem));
    }

    private void populateListItem(List<String> listItem) {
//        SQLiteDatabase db = getActivity().openOrCreateDatabase(SqlHealthManagerDatabaseHelper.DATABASE_NAME, 0, null);
//
//        Cursor c = db.rawQuery("select * from "+ CategoryTable.TABLE_NAME, null);
//        if(c.moveToFirst()){
//            do{
//                System.out.println(c.getString(c.getColumnIndex(CategoryTable.CATEGORY_NAME)));
//            }while (c.moveToNext());
//        }

        listItem.add(this.getActivity().getResources().getString(R.string.click_to_add_item));
        //listItem.add("item2");
        //listItem.add("item3");
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
//                if(position == 0){
//                    addNewItem();
//                }
        launchNewActivity(position);

    }

    private void launchNewActivity(int index) {
        Intent intent = new Intent();
        intent.setClass(getActivity(), ItemEntryActivity.class);
        intent.putExtra("index", index);
        startActivity(intent);
    }

    private void addNewItem() {
        SQLiteDatabase db = getActivity().openOrCreateDatabase(SqlHealthManagerDatabaseHelper.DATABASE_NAME, 0, null);


    }
}
